package dp.test44;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/22 11:20
 */
public class Solution1 {

    public boolean isMatch(String s, String p) {
        if (p.equals("")) return s.equals("");
        int m = s.length(), n = p.length();
        boolean[][] dp = new boolean[m + 1][n + 1];
        dp[0][0] = true;
        // 首先考虑s为空的时候
        for (int j = 1; j <= n; j++) {
            if (p.charAt(j - 1) == '*') {
                dp[0][j] = dp[0][j - 1];
            }
        }
        // 整体考虑
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '?') {
                    dp[i][j] = dp[i - 1][j - 1];
                } else if (p.charAt(j - 1) == '*') {
                    // 匹配空串 ， 匹配单个字符， 匹配多个字符
                    dp[i][j] = dp[i][j - 1] || dp[i - 1][j - 1] || dp[i - 1][j];
                }
            }
        }
        return dp[m][n];
    }
}
